package cn.com.bob.library.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.bob.library.entity.Borrow;
import cn.com.bob.library.service.BorrowService;
import cn.com.bob.library.mapper.BorrowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 12948
* @description 针对表【borrow】的数据库操作Service实现
* @createDate 2023-06-30 08:27:12
*/
@Service
public class BorrowServiceImpl extends ServiceImpl<BorrowMapper, Borrow>
    implements BorrowService{

    @Autowired
    private BorrowMapper borrowMapper;

    /**
     * 如果存在，则返回true
     * @param userId:
     * @param bookId:
     * @return boolean
     */
    @Override
    public Borrow existUserAndBook(Integer userId, Integer bookId) {
        return borrowMapper.selectOne(new QueryWrapper<Borrow>().eq("user_id", userId).eq("book_id", bookId));
    }

    /**
     * 书籍数加1
     * @param borrow:
     * @return void
     */
    @Override
    public void addBookSum(Borrow borrow) {
        borrow.setQuantity(borrow.getQuantity() + 1);
        borrowMapper.update(borrow,new QueryWrapper<Borrow>().eq("user_id",borrow.getUserId()).eq("book_id",borrow.getBookId()));
    }

    /**
     * 新增一条关系链
     * @param userId:
     * @param bookId:
     * @return void
     */
    @Override
    public void addBorrow(Integer userId, Integer bookId,Integer categoryId) {
        Borrow borrow = new Borrow(userId,bookId,1,categoryId,new Date(),null);
        borrowMapper.insert(borrow);
    }

    /**
     * 根据用户id获取所有的借阅关系
     * @param userId:
     * @return List<Borrow>
     */
    @Override
    public List<Borrow> getAllBorrowByUserId(Integer userId) {
        return borrowMapper.selectList(new QueryWrapper<Borrow>().eq("user_id", userId));
    }
}




